﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using LevelEditor.App;

namespace LevelEditor
{
    public partial class LoadImageSequence : Form
    {
        public enum Mode
        {
            View, Load, SelectImage,
        }
        private List<Image> loadedImages;
        private DialogResult result;
        private bool wantToClose;

        private Mode currentMode;
        public Mode CurrentMode
        {
            get { return currentMode; }
            
        }

        private PictureBox selectedImage;
        public Image SelectedImage
        {
            get { return selectedImage.Image; }

        }

        private int indexInBehaviorList;
        public int IndexInBehaviorList
        {
            get { return indexInBehaviorList; }
        }

        public LoadImageSequence()
        {
            InitializeComponent();

            this.currentMode = Mode.Load;
            this.FormClosing += LoadImageSequence_FormClosing;
            wantToClose = false;

            loadedImages = new List<Image>();

            OpenFileDialog loadImageSequence = new OpenFileDialog();
            loadImageSequence.Multiselect = true;
            loadImageSequence.Filter = "PNG Images: |*.png";

            if (loadImageSequence.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FillImagesDisplayer(loadImageSequence.FileNames);
            }
            else
            {
                
                this.Close();
            }

            result = System.Windows.Forms.DialogResult.Cancel;
        }

        /// <summary>
        /// Constructor ONLY to view images
        /// </summary>
        /// <param name="imagesToView">A list of images to show</param>
        public LoadImageSequence(List<Image> imagesToView, bool selectingImage)
        {
            InitializeComponent();
            if (selectingImage)
                this.currentMode = Mode.SelectImage;
            else
                this.currentMode = Mode.View;

            this.FormClosing += LoadImageSequence_FormClosing;
            wantToClose = false;

            FillImagesDisplayer(imagesToView);
        }

        

        void LoadImageSequence_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && !wantToClose)
            {
                result = System.Windows.Forms.DialogResult.Cancel;
            }
        }

        private void FillImagesDisplayer(string[] filePaths)
        {
            for (int k = 0; k < filePaths.Length; k++)
            {
                if (filePaths.Equals(""))
                {
                    ErrorController.ShowOkError(ErrorController.LevelEditorError.APP_IMGSEQUENCE_INVALID);
                    return;
                }
                try
                {
                    FileStream fs = new FileStream(filePaths[k], FileMode.Open);

                    Image image = Bitmap.FromStream(fs);
                    fs.Close();

                    loadedImages.Add(image);

                    PictureBox thumb = new PictureBox();
                    thumb.Image = image.GetThumbnailImage(image.Width - (int)(image.Width * 0.4f), image.Height - (int)(image.Height * 0.4f), null, IntPtr.Zero);
                    thumb.SizeMode = PictureBoxSizeMode.StretchImage;

                    imagesDisplayer.Controls.Add(thumb);
                }
                catch (Exception e)
                {
                    ErrorController.ShowOkError(ErrorController.LevelEditorError.APP_IMGSEQUENCE_LOAD);
                }
            }
        }

        private void FillImagesDisplayer(List<Image> images)
        {
            for (int k = 0; k < images.Count; k++)
            {
                PictureBox thumb = new PictureBox();
                thumb.Image = images[k].GetThumbnailImage(images[k].Width - (int)(images[k].Width * 0.4f), images[k].Height - (int)(images[k].Height * 0.4f), null, IntPtr.Zero);
                thumb.SizeMode = PictureBoxSizeMode.StretchImage;
                if (this.currentMode == Mode.SelectImage) 
                    thumb.Click += thumb_Click;

                imagesDisplayer.Controls.Add(thumb);
            }
        }

        void thumb_Click(object sender, EventArgs e)
        {
            if (selectedImage != null)
                selectedImage.BorderStyle = BorderStyle.None;
            selectedImage = ((PictureBox)sender);

            indexInBehaviorList = this.imagesDisplayer.Controls.GetChildIndex(selectedImage);

            ((PictureBox)sender).BorderStyle = BorderStyle.FixedSingle;

        }

        private void okBtn_Click(object sender, EventArgs e)
        {
            wantToClose = true;
            this.result = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.result = System.Windows.Forms.DialogResult.Cancel;
            wantToClose = true;
            this.Close();
        }

        public List<Image> LoadedImages
        {
            get { return loadedImages; }
        }

        public new DialogResult ShowDialog()
        {
            base.ShowDialog();

            return result;
        }
    }
}
